<?php

namespace Xrfg\Api\Trading\Spot\Console;

use Illuminate\Console\Command;
use Xrfg\Api\Trading\Spot\Jobs\MatchJob;
use Xrfg\Core\Trading\Spot\Models\Order;
use Xrfg\Api\Trading\Spot\Repositories\OrderBook;

class RebuildOrderBook extends Command
{
    public $signature = 'orderbook:rebuild';

    public $description = '重新构建订单簿';

    public function handle(OrderBook $orderBook)
    {
        $orders = Order::where('status', Order::STATUS_NEW)
            ->get();

        $bar = $this->output->createProgressBar($orders->count());
        $bar->start();

        $orders->each(function (Order $order) use ($bar, $orderBook) {
            $orderBook->add($order);
            MatchJob::dispatch($order->symbol)
                ->onQueue('match:'.$order->symbol);

            $bar->advance();
        });

        $bar->finish();
    }
}
